﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class page_Brand_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {     
        if(!Page.IsPostBack)
        {
            ReadAction();
            SetPageNoCache();
        }      
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.AdminTrainingInformation);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string fileName = "";
        string url = "~/UpFile/Brand/";
        DAL.Tables.tbl_Shop_Brand b = new DAL.Tables.tbl_Shop_Brand();

        if (this.File1.Value.Trim() != "")
        {
            if (Shove._IO.File.UploadFile(this.Page, this.File1, url, ref fileName, false, "") < 0)
            {
                //Shove._Web.JavaScript.Alert(this.Page, "文件上传失败");
            }

            b.BrandPic.Value = fileName;
        }

        b.BrandName_CN.Value = txtCHName.Text;
        b.BrandName_EN.Value = txtENName.Text;       
        b.Remark.Value = txtRemark.Text;
        b.Introduce.Value = txtIntroduce.Text;
        b.Isuse.Value = 1;

        //是否自销品牌
        if (cbIsSelf.Visible == true && cbIsSelf.Checked == false)
        {
            b.ShopID.Value = _Site.ID;
        }

        if (this.hAction.Value == "Edit")
        {
            if (b.Update("ID=" + hID.Value) >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改成功！", "Brand_Manager.aspx");
            }
        }

        if (this.hAction.Value == "Add")
        {
            if (txtCHName.Text != null)
            {
                if (b.Insert() >= 0)
                {
                    Shove._Web.JavaScript.Alert(this.Page, "保存成功", "Brand_Manager.aspx");
                }
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "品牌名称不能为空！");
            }
        }
    }

    protected void btnchanle_Click(object sender, EventArgs e)
    {
        Response.Redirect("Brand_Manager.aspx");  
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    string Action = Shove._Web.Utility.GetRequest("Action");
        //    if (Action == "Edit")
        //    {
        //        int id = int.Parse(Shove._Web.Utility.GetRequest("ID");
        //        if (id > 0)
        //        {
        //            Brand b = BrandBLL.GetBrandByID(id);
        //            BLL.BrandBLL.UpLoadImage(b);
        //            Shove._Web.JavaScript.Alert(this.Page, "修改图片成功");
                    
        //        }
        //    }
        //    if (Action == "Add")
        //    {
        //        Shove._Web.JavaScript.Alert(this.Page, "还没有上传图片，请保存后在进行编辑");
        //    }
           
        //}
        //catch(Exception exc) 
        //{
        //    Shove._Web.JavaScript.Alert(this.Page, "修改图片失败");

        //}

    }

    private void ReadAction()
    {
        String sAction = Shove._Web.Utility.GetRequest("Action");
        this.hAction.Value = sAction;
        
        String sID = Shove._Web.Utility.GetRequest("ID");
        this.hID.Value = sID;

        if (this.hAction.Value == "Edit")
        {
            BindData();

        }
        if (this.hAction.Value == "Add")
        {
            this.Image1.Visible = false;
        }
    }

    private void BindData()
    {
        if (this.hAction.Value == "Edit")
        {
            int ID = int.Parse(Shove._Web.Utility.GetRequest("ID"));
            DataTable dt = new DAL.Tables.tbl_Shop_Brand().Open("", "ID=" + ID, "");
            long SiteID = Shove._Convert.StrToLong(dt.Rows[0]["ShopID"].ToString(), 0);

            if (SiteID != -1)
            {
                cbIsSelf.Checked = true;
            }

            txtCHName.Text = dt.Rows[0]["BrandName_CN"].ToString();
            txtENName.Text = dt.Rows[0]["BrandName_EN"].ToString();
            txtRemark.Text = dt.Rows[0]["Remark"].ToString();
            txtIntroduce.Text = dt.Rows[0]["Introduce"].ToString();

            string fileName = dt.Rows[0]["BrandPic"].ToString();
            string url = "~/UpFile/Brand/" + fileName;
            Image1.ImageUrl = url;
        }
    }

    #region 设置页面不被缓存

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }

    #endregion
}
